package com.demo.java.net.inetaddress;

import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by carlkong on 2016/5/26.
 */
public class UnitTest {
    @Test
    public void testInetAddress() {

        try {
            //获取本机的InetAddress实例
            InetAddress address = InetAddress.getLocalHost();
            address.getHostName();//获取计算机名
            address.getHostAddress();//获取IP地址
            byte[] bytes = address.getAddress();//获取字节数组形式的IP地址,以点分隔的四部分

            //获取其他主机的InetAddress实例 可以通过主机名或者ip来获得InetAddress
//            InetAddress address2 = InetAddress.getByName("carlkong-PC");
            InetAddress address2 = InetAddress.getByName("ANDY_PC");
            InetAddress address3 = InetAddress.getByName("192.168.6.184");
            System.out.println("address："+address);
            System.out.println("bytes[]:"+bytes.toString());
            System.out.println("address2："+address2);
            System.out.println("address3："+address3);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }
}
